package com.lovo.project.service.impl;

import com.lovo.project.dao.UserDAO;
import com.lovo.project.dto.UserDetailDTO;
import com.lovo.project.dto.UserLoginDTO;
import com.lovo.project.enums.UserCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PermissionDO;
import com.lovo.project.model.UserDO;
import com.lovo.project.service.UserService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/3 11:23
 */
public class UserServiceImpl implements UserService {

    @Override
    public void register(UserDO userDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);

        try {
            int i = dao.insertUser(userDO);
            if (i<=0){
                throw new OurException(UserCodeEnum.USER_REGISTER_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public UserDO login(String userName, String password) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);
        try {
            UserDO login = dao.selectUserByUsernameAndPassword(userName,password);
            if (login == null){
                throw new OurException(UserCodeEnum.USER_LOGIN_ERROR);
            }
            return login;
        } catch (OurException e) {
           throw e;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);

        try {
            int i = dao.deleteUserById(userId);
            if (i<=0) {
                throw  new OurException(UserCodeEnum.USER_DELETE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
           sqlSession.rollback();
           throw e;
        } finally {
            sqlSession.close();
        }


    }

    @Override
    public UserDO findByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);

        try {
            UserDO user = dao.selectUserById(userId);
            if (user == null) {
                throw new OurException(UserCodeEnum.USER_FIND_ERROR);
            }
            sqlSession.commit();
            return user;
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public List<UserDO> listByInfo(String type, String info) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);
        try {
            List<UserDO> list = dao.selectByInfo(type, info);
            if (list == null){
                throw new OurException(UserCodeEnum.USER_LOGIN_ERROR);
            }
            return list;
        } catch (OurException e) {
            throw e;
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public void updateUser(Integer userId, UserDO userDO) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);

        try {
            int i = dao.updateUserById(userId, userDO);
            if (i<=0) {
                throw new OurException(UserCodeEnum.USER_UPDATE_ERROR);
            }
            sqlSession.commit();
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }


    }

    @Override
    public List<PermissionDO> findPermissionByUserId(Integer userId) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        UserDAO dao = sqlSession.getMapper(UserDAO.class);

        try {
            List<PermissionDO> list = dao.selectPermissionByUserId(userId);
            if (list == null) {
                throw new OurException(UserCodeEnum.USER_FIND_ERROR);
            }
            sqlSession.commit();
            return list;
        } catch (OurException e) {
            sqlSession.rollback();
            throw e;
        } finally {
            sqlSession.close();
        }
    }


}
